import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_select_function.dart';
import 'package:enterprise_v6_app/app/core/constans/dict_data_info.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/common/choice_bean.dart';
import 'package:enterprise_v6_app/app/data/entities/application/emergency/emergency_res_goods_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/get_dict_all_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/emergency/resource/model.dart';

/// 应急物资
class EmergencyResourceGoodsController extends GetxController {
  final popRightScaffoldKey = GlobalKey<ScaffoldState>();

  /// 搜索使用key
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();

  String? search;

  /// 评估状态
  EmergencyAssessStatus? assess;

  /// 区域
  List<SimpleChoiceBean> areas = [];
  SimpleChoiceBean? area;

  /// 物资类型
  List<SimpleChoiceBean> goodsTypes = [];
  SimpleChoiceBean? goodsType;

  @override
  void onInit() {
    super.onInit();
    initData();
  }

  /// 初始化数据
  Future<void> initData() async {
    final List<GetDictAllEntity> dictTypeList = await DictDataInfo.getDictionaryDataByTree(dictionaryType: 'yjwzqy_3K3F5POJ');
    areas.addAll(dictTypeList.map((e) => SimpleChoiceBean(e.dictionaryLabel!, e.id!)));

    final List<GetDictAllEntity> dictTypeList2 = await DictDataInfo.getDictionaryDataByTree(dictionaryType: 'yjwzfl_F5b9AoN7');
    goodsTypes.addAll(dictTypeList2.map((e) => SimpleChoiceBean(e.dictionaryLabel!, e.id!)));
  }

  Future<List<EmergencyResGoodsEntity>> getListData(int pageNum, int pageSize) async {
    final res = await HttpUtil().generateGetRequest<List<EmergencyResGoodsEntity>>(
      '${Interface.mainBaseUrl}/emergency//EmergencySupplies/getMaterialPointsPage',
      queryParameters: {
        'pageNum': pageNum,
        'pageSize': pageSize,
        'nameOfMaterialPoint': search,
        'assess': assess,
        'locationOfTheRegion': area?.name,
      },
    ).execute();
    return res.data ?? [];
  }

  /// 筛选
  void screen() {
    popRightScaffoldKey.currentState?.openEndDrawer();
  }

  /// 搜索
  void refreshPage() {
    globalKey.currentState?.onRefresh();
  }

  /// 显示选择评估状态
  void showSelect() {
    CustomSelectFunction.selectMultipleBottomSheet<EmergencyAssessStatus>(
      onCallBack: (val) {
        assess = val.first;
        update();
      },
      dataList: EmergencyAssessStatus.assessList,
      checkedList: assess == null ? [] : [assess!],
      isMultiple: false,
    );
  }

  /// 显示选择区域
  void showSelectArea() {
    CustomSelectFunction.selectMultipleBottomSheet<SimpleChoiceBean>(
      onCallBack: (val) {
        area = val.first;
        update();
      },
      dataList: areas,
      checkedList: area == null ? [] : [area!],
      isMultiple: false,
    );
  }

  /// 显示选择物资类型
  void showSelectGoodsType() {
    CustomSelectFunction.selectMultipleBottomSheet<SimpleChoiceBean>(
      onCallBack: (val) {
        goodsType = val.first;
        update();
      },
      dataList: goodsTypes,
      checkedList: goodsType == null ? [] : [goodsType!],
      isMultiple: false,
    );
  }

  void clearChecked() {
    assess = search = area = goodsType = null;
    update();
  }

  void confirm() {
    popRightScaffoldKey.currentState?.closeEndDrawer();
    refreshPage();
  }
}
